﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScoreMS.Model;

namespace ScoreMS.BLL
{
    /// <summary>
    /// 课程实施记录，事务逻辑
    /// </summary>
    public class Course_logBS
    {
        /// <summary>
        /// 获取单个记录
        /// </summary>
        /// <param name="course_id"></param>
        /// <returns></returns>
        static public Course_log GetCourse_log(int log_id)
        {
            using (scoremsdbEntities db = new scoremsdbEntities())
            {
                Course_log model = new Course_log();
                var course_log = db.course_log.FirstOrDefault(x => x.log_id == log_id);
                model.log_id = course_log.log_id;
                model.log_index = course_log.log_index;
                model.log_form = course_log.log_form;
                model.syllabus_id = course_log.syllabus_id;
                model.teacher_id = course_log.teacher_id;
                model.course_id = course_log.course_id;
                return model;
            }
        }

        /// <summary>
        /// 获取指定教师指定课程的记录列表
        /// </summary>
        /// <returns></returns>
        static public List<Course_log> GetCourse_logList(string teacher_id, string course_id)
        {
            using (scoremsdbEntities db = new scoremsdbEntities())
            {
                //按log_index排列，升序
                var ret = db.course_log.Where(x => x.teacher_id == teacher_id && x.course_id == course_id).OrderBy(c=>c.log_index);
                List<Course_log> list = new List<Course_log>();
                foreach (var course_log in ret)
                {
                    Course_log model = new Course_log();
                    model.log_id = course_log.log_id;
                    model.log_index = course_log.log_index;
                    model.log_form = course_log.log_form;
                    model.syllabus_id = course_log.syllabus_id;
                    model.teacher_id = course_log.teacher_id;
                    model.course_id = course_log.course_id;
                    list.Add(model);
                }
                return list;
            }
        }

        /// <summary>
        /// 获取指定课程的教学大纲列表
        /// </summary>
        /// <param name="course_id"></param>
        /// <returns></returns>
        static public List<Course_syllabus> GetCourse_syllabusList(string course_id)
        {
            return Course_syllabusBS.GetCourse_syllabusList(course_id);
        }

        /// <summary>
        /// 添加记录
        /// </summary>
        /// <returns>成功返回true</returns>
        static public bool AddCourse_log(Course_log model)
        {
            try
            {
                using (scoremsdbEntities db = new scoremsdbEntities())
                {
                    var course_log = new course_log()
                    {                        
                        log_index = model.log_index,
                        log_form = model.log_form,
                        teacher_id = model.teacher_id,
                        course_id = model.course_id,
                        syllabus_id = model.syllabus_id
                    };
                    db.course_log.AddObject(course_log);
                    db.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
